//账号找回
const App = Vue.createApp({
    data() {
        return {
            Type: 1, //1=QQ 2=手机号
            QQData: {
                qrsig: '', //二维码签名
                image: '', //二维码
                message: 'QQ二维码正在获取中!', state: false, //二维码获取状态
            }, count: 1, //监听次数
            CaptchaType: CaptchaType, //验证码类型，8=极验，其他=图片
            GtCaptchaId: GtCaptchaId, //极验验证ID
            cellphone: '', //手机号
            vercode: '', //短信验证码
        }
    }, mounted() {
        this.QQCodeGet();
        if (this.CaptchaType === 8) {
            this.addScript(ROOT_DIR_S + '/assets/js/gt4.js');
        }
        this.LoginStatus();
    }, methods: {
        checkTelephone(telephone) {
            var reg = /^1[3-9][0-9]{9}$/;
            if (!reg.test(telephone)) {
                return false;
            } else {
                return true;
            }
        }, //极验验证集成
        CaptchaGet(name_id) {
            let _this = this;
            if (App.cellphone === '' || !this.checkTelephone(App.cellphone)) {
                layer.msg('请输入正确的手机号', {icon: 2, anim: 6});
                return false;
            }
            //初始化验证
            let captchaBox = document.getElementById(name_id);
            initGeetest4({
                captchaId: _this.GtCaptchaId, product: 'bind', language: 'zho', userInfo: name_id, //验证类型,必须提交和验证时一致
            }, function (captchaObj) {
                captchaObj.reset();
                captchaObj.showBox();
                captchaObj.appendTo(captchaBox).onSuccess(function (e) {
                    var result = captchaObj.getValidate();
                    if (!result) {
                        alert('请先完成验证！');
                        window.location.reload();
                        return false;
                    }
                    result.name = name_id; //判断验证类型
                    _this.SendVerificationCode(result);
                })
            });
        }, //验证短信是否正确
        CodeLogin() {
            if (App.vercode === '') {
                return false;
            }
            layer.msg('正在找回中,请稍后', {icon: 16, time: 9999999});
            //验证短信是否正确
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=Send_verification_login', data: {
                    code: App.vercode, type: 2
                }, type: 'POST', dataType: 'json', success: (res) => {
                    layer.closeAll();
                    if (res.code >= 1) {
                        swal('您的账号信息如下', res.msg);
                        let content = `
                                <div style="text-align: left;">
                                    <div style="text-align: center;"><img src="${res.data.image}" style="width: 68px;height: 68px;border-radius: 68px;margin-bottom: 1em;box-shadow: 3px 3px 12px #ccc;" /></div>
                                    <div>用户名称：${res.data.name}</div>
                                    <div>登录账号：${res.data.username}</div>
                                    <div>上次登录：${res.data.lasttime}</div>
                                    <div>注册时间：${res.data.addtime}</div>
                                    <hr>
                                    <div>${res.msg}</div>
                                </div>
                                `;
                        $(".swal-text").html(content);
                        $(".swal-button").text('好的！');
                        $(".swal-modal").css('vertical-align', 'baseline');
                        $(".swal-footer").css('text-align', 'center');
                    } else {
                        swal(res.msg, '', 'error');
                    }
                }
            });
        }, //发送验证码
        SendVerificationCode(gt_data = {}) {
            let data = {
                mobile: App.cellphone, GT: gt_data
            }
            if (App.cellphone === '' || !this.checkTelephone(App.cellphone)) {
                swal('请输入正确的手机号', '', 'error');
                return false;
            }
            if (this.CaptchaType !== 8) {
                let image = ROOT_DIR_S + '/user/ajax.php?act=VerificationCode&n=Login_sms_vis&t=';
                layer.open({
                    type: 1,
                    title: '请输入验证码',
                    content: App.ContentHtml(image),
                    btn: ['确定', '取消'],
                    yes: function () {
                        var code = $("#captcha").val();
                        if (code == '') {
                            swal('请输入验证码', '', 'error');
                            return false;
                        }
                        layer.closeAll();
                        data.code = code;
                        data.GT = null;
                        App.SendVerificationCodeAjax(data);
                    },
                    success: function () {
                        $("#Image").click(function () {
                            $("#Image").attr('src', image + Math.random());
                        })
                    }
                })
            } else {
                App.SendVerificationCodeAjax(data);
            }
        }, SendVerificationCodeAjax(data) {
            let _this = this;
            data.type = 2; //2=密码找回模式
            layer.msg('正在发送中,请稍后', {icon: 16, time: 9999999});
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=Send_verification_code_login',
                data: data,
                type: 'POST',
                dataType: 'json',
                success: (res) => {
                    layer.closeAll();
                    if (res.code >= 1) {
                        //验证码发送成功
                        _this.vercode = '';
                        swal(res.msg, '', 'success');
                    } else {
                        swal(res.msg, '', 'error');
                    }
                }
            });
        }, //获取QQ二维码
        LoginStatus() {
            //账号登录检测，5秒一次，如果处于登录状态，自动刷新页面！
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=LoginStatus',
                type: 'POST',
                dataType: 'json',
                success: (res) => {
                    if (res.code === 1) {
                        TopLogin(HREF);
                    } else {
                        setTimeout(function () {
                            App.LoginStatus();
                        }, 5000);
                    }
                }
            })
        },
        QQCodeGet(type = 1) {
            if (type == 2) {
                window.location.reload();
                return;
            }
            let _this = this;
            layer.load(2, {shade: [0.1, '#000000'], time: 9999999});
            this.QQData.message = "QQ二维码正在获取中!";
            this.QQData.state = false;
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=QQCode', type: 'POST', dataType: 'json', success: (res) => {
                    layer.closeAll();
                    if (res.code === 1) {
                        _this.QQData.message = res.msg;
                        _this.QQData.qrsig = res.data.qrsig;
                        _this.QQData.image = res.data.image;
                        _this.QQData.state = true;
                        _this.count = 1;
                        _this.QQCodeState(); //开始监听扫码状态
                    } else {
                        _this.QQData.state = false;
                        _this.QQData.message = res.msg;
                        swal(res.msg, '', 'error');
                    }
                }
            })
        }, //监听扫码状态
        QQCodeState() {
            if (this.Type === 2) {
                return false;
            }
            let _this = this;
            this.QQData.message = this.QQData.message + '...';
            $.ajax({
                url: ROOT_DIR_S + '/user/ajax.php?act=QQCodeState', type: 'POST', dataType: 'json', data: {
                    qrsig: _this.QQData.qrsig
                }, success: (res) => {
                    switch (res.code) {
                        case 1: //正常状态，还没扫码成功
                            _this.QQData.message = res.msg + '(监听:' + _this.count + '次)';
                            setTimeout(() => {
                                _this.count += 1;
                                _this.QQCodeState();
                            }, 2500);
                            break;
                        case 2: //扫码成功,登录成功！
                            _this.QQData.message = res.msg + '(监听:' + _this.count + '次)';
                            swal('您的账号信息如下', res.msg);
                            let content = `
                                <div style="text-align: left;">
                                    <div style="text-align: center;"><img src="${res.data.image}" style="width: 68px;height: 68px;border-radius: 68px;margin-bottom: 1em;box-shadow: 3px 3px 12px #ccc;" /></div>
                                    <div>用户名称：${res.data.name}</div>
                                    <div>登录账号：${res.data.username}</div>
                                    <div>上次登录：${res.data.lasttime}</div>
                                    <div>注册时间：${res.data.addtime}</div>
                                    <hr>
                                    <div>${res.msg}</div>
                                </div>
                                `;
                            $(".swal-text").html(content);
                            $(".swal-button").text('好的！');
                            $(".swal-modal").css('vertical-align', 'baseline');
                            $(".swal-footer").css('text-align', 'center');
                            break;
                        default: //失败状态
                            _this.QQData.state = false;
                            _this.QQData.message = res.msg + '(监听:' + _this.count + '次)';
                            swal(res.msg, '', 'error');
                            break;
                    }
                }
            })
        }, addScript(url) {
            var script = document.createElement('script');
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('src', url);
            document.getElementsByTagName('head')[0].appendChild(script);
        }, ContentHtml(image) { //验证码弹窗
            return `<style>.layui-layer-page .layui-layer-content{overflow:hidden;}</style>
                    <div class="layui-form" style="padding:1em 1em 0 1em;max-width:320px;">
                        <div class="layui-form-item">
                            <div class="layui-row" style="width: 94%;margin:auto;">
                                <div class="layui-col-xs12">
                                    <div class="layui-input-wrap">
                                        <div class="layui-input-prefix">
                                            <i class="layui-icon layui-icon-vercode"></i>
                                        </div>
                                        <input type="text" id="captcha" value="" style="min-width:auto;" placeholder="图片验证码"
                                               lay-reqtext="请填写验证码" class="layui-input" >
                                    </div>
                                </div>
                                <div class="layui-col-xs12">
                                    <div  style="text-align: center;margin-top: 1em;cursor: pointer;">
                                        <img src="` + image + `" style="width:150px;" id="Image">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                `;
        }
    }
}).mount('#App');

function TopLogin(url) {
    url += ROOT_DIR_S;
    if (window.self !== window.top) {
        if (GID >= 1) {
            top.location.href = url + '/?mod=route&p=Goods&gid=' + GID;
        } else if (REQUEST === 'user') {
            top.location.href = url + '/?mod=route&p=User';
        } else {
            top.location.reload();
        }
    } else {
        if (GID >= 1) {
            window.location.href = url + '/?mod=route&p=Goods&gid=' + GID;
        } else if (REQUEST === 'user') {
            window.location.href = url + '/?mod=route&p=User';
        } else {
            window.location.href = url;
        }
    }
}